package com.javabasic.algorithm.sword_of_offer;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/8 10:43 下午
 */
public class 剑指_Offer_53_II_0_n_1中缺失的数字 {

    /**
     * 等差数列前n项和公式
     * @param nums
     * @return
     */
    public int missingNumber(int[] nums) {
        int len = nums.length;
        if (nums[len-1] != len) {
            return len;
        } else {
            int sum = len * (len + 1) / 2;
            for (int i = 0; i < len; i++) {
                sum -= nums[i];
            }
            return sum;
        }
    }


    /**
     * 二分查找法
     * @param nums
     * @return
     */
    public int missingNumber2(int[] nums) {
        int len = nums.length;
        if (nums[len-1] != len) {
            return len;
        } else {
            int sum = len * (len + 1) / 2;
            for (int i = 0; i < len; i++) {
                sum -= nums[i];
            }
            return sum;
        }
    }
}
